# -*- mode: makefile;-*-
#
# Copyright (C) 1999-2020 Apple Inc. All rights reserved.
#
# MakeInc.rule defines the targets and rules for
# leaf directories once MakeInc.dir has recursed
# into them. "do_XXX" may be double-colon rules
# to allow the Makefile in the source directory
# to augment the actions that will be performed.
#
include $(SRCROOT)/makedefs/MakeInc.color

#
# Empty the export lists of headers when building for exclaves
#

ifeq ($(EXPORTHDRS_SKIP_EXCLAVES),YES)
EXPORT_MI_LIST =
EXPORT_MI_GEN_LIST =
EXPORT_MD_LIST =
EXPORT_MD_GEN_LIST =
endif

#
# Generic Install rules
#

ifndef INSTALL_KF_MI_LCL_LIST
    INSTALL_KF_MI_LCL_LIST = $(EXPORT_MI_LIST)
endif

ifndef INSTALL_KF_MI_LCL_GEN_LIST
    INSTALL_KF_MI_LCL_GEN_LIST = $(EXPORT_MI_GEN_LIST)
endif

ifndef INSTALL_KF_MD_LCL_LIST
    INSTALL_KF_MD_LCL_LIST = $(EXPORT_MD_LIST)
endif

ifndef INSTALL_KF_MD_LCL_GEN_LIST
    INSTALL_KF_MD_LCL_GEN_LIST = $(EXPORT_MD_GEN_LIST)
endif

ifndef INSTALL_KF_MI_LIST
    INSTALL_KF_MI_LIST = $(EXPORT_MI_LIST)
endif

ifndef INSTALL_KF_MI_GEN_LIST
    INSTALL_KF_MI_GEN_LIST = $(EXPORT_MI_GEN_LIST)
endif

ifndef INSTALL_KF_MD_LIST
    INSTALL_KF_MD_LIST = $(EXPORT_MD_LIST)
endif

ifndef INSTALL_KF_MD_GEN_LIST
    INSTALL_KF_MD_GEN_LIST = $(EXPORT_MD_GEN_LIST)
endif

$(eval $(call _setup_build_log_colors))

.PHONY: ALWAYS

ALWAYS:

# $(1) is the list of install paths
# $(2) is "1" if it's a "GEN"-style rule that looks locally, or else $(SOURCE)
# $(3) is the local temp directory for processing
# $(4) is the unifdef flags
#
# $$$$$$$$ is a double-escaped "$$" to represent the current pid
# of the shell process for creating uniquely named temporary files

define INSTALLHDRS_RULE_template

.PHONY: $(3)_MKDIR

$(3)_MKDIR:
	$$(_v)$$(MKDIR) ./$(3)
	$$(_v)$$(MKDIR) $(dir $(firstword $(1)))

# Rebuild if unifdef flags change
$(1): $(3)/.UNIFDEF_FLAGS
$(3)/.UNIFDEF_FLAGS: ALWAYS | $(3)_MKDIR
	$$(_v)$$(REPLACECONTENTS) $$@ $$(UNIFDEF) $(4)

$(1): $(dir $(firstword $(1)))% : $(if $(2),%,$$(SOURCE)/%) | $(3)_MKDIR
	@$$(LOG_INSTALLHDR) "$$*"
	$$(_v)$$(UNIFDEF) $(4) $$< > ./$(3)/$$*.unifdef.$$$$$$$$;		\
	if [ $$$$? -eq 2 ]; then						\
		$(ERR) Parse failure for $$<;					\
		exit 1;								\
	fi;									\
	$$(DECOMMENT) ./$(3)/$$*.unifdef.$$$$$$$$ r >				\
		./$(3)/$$*.strip.$$$$$$$$ || exit 1;				\
	if [ -s ./$(3)/$$*.strip.$$$$$$$$ ]; then				\
		$$(INSTALL) $$(INSTALL_FLAGS) ./$(3)/$$*.unifdef.$$$$$$$$ $$@ || exit 1;	\
	fi;									\
	$$(RM) ./$(3)/$$*.unifdef.$$$$$$$$ ./$(3)/$$*.strip.$$$$$$$$
endef

# $(1) is the list of install paths
# $(2) is the source path pattern (using % to match with $(5)) or source file
# $(3) is the local temp directory for processing
# $(4) is the unifdef flags
# $(5) is the destination directory (when pattern matching) or empty
#
# $$$$$$$$ is a double-escaped "$$" to represent the current pid
# of the shell process for creating uniquely named temporary files

define INSTALLPYTHON_RULE_template

.PHONY: $(3)_MKDIR

$(3)_MKDIR:
	$$(_v)$$(MKDIR) ./$(3)

# Rebuild if unifdef flags change
$(1): $(3)/.UNIFDEF_FLAGS
$(3)/.UNIFDEF_FLAGS: ALWAYS | $(3)_MKDIR
	$$(_v)$$(REPLACECONTENTS) $$@ $$(UNIFDEF) -t $(4)

$(1): $(5)% : $(2) | $(3)_MKDIR
	@$$(LOG_INSTALLPY) "$$*"
	$$(_v)$$(MKDIR) $$(dir $$@) $$(dir ./$(3)/$$*)
	$$(_v)$$(UNIFDEF) -t $(4) $$< > ./$(3)/$$*.unifdef.$$$$$$$$$$(suffix $$*); \
	if [ $$$$? -eq 2 ]; then						\
		$(ERR) Parse failure for $$<;				\
		exit 1;							\
	fi;								\
	$$(INSTALL) $$(DATA_INSTALL_FLAGS) \
		./$(3)/$$*.unifdef.$$$$$$$$$$(suffix $$*) $$@ || exit 1; \
	$$(PYTHON) $$(LLDBMACROS_SOURCE)/lint/syntax_checker.py \
		./$(3)/$$*.unifdef.$$$$$$$$$$(suffix $$*) $$(_vstdout) || exit 1; \
	$$(RM) ./$(3)/$$*.unifdef.$$$$$$$$$$(suffix $$*)
	$$(_v)if [ -n "$(5)" ]; then $$(TOUCH) "$(5)"; fi
endef

# Template for Libsyscall header unifdef postprocessing in DSTROOT
# $(1) is the install path
# $(2) is the unifdef flags

define LIBSYSCALL_DO_UNIFDEF_HEADERS_RULE_template
do_unifdef_headers::
	$$(_v)[ \! -d "$(1)" ] || $$(FIND) -x "$(1)"  -type f \( -name \*.h -o \
		-name \*.defs -o -name \*.iig -o -name \*.modulemap \) \
		-execdir $$(UNIFDEF) $(2) -o {} {} \;
.PHONY: do_unifdef_headers
endef

# Helper function to call a verifier
# Generally, a verifier is a function that post-processes a .o file to validate
# some characteristics.
#
# This ensures that the output .o file is renamed if the verifier rejects the
# input or fails. This forces the .o file to be re-generated & re-verified if
# another build is run.
#
# Note: currently, it is possible to end up in an inconsistent state by
# Ctrl+C-ing in the middle of a verifier run, causing a .o file that would
# have failed verification to still exist as a .o instead o a .o.unverified.
# The next build should be a "clean" build to prevent the .o file avoiding
# verification.
#
# Any stderr output from the verifier is printed with an "error:" prefix.
#
# $(1) is the name of the verifier to call
# $(2) is the name of the .o file to pass to the verifier
define VERIFY
{ ERROR_OUTPUT="$$({ $(call $(1),$(2)) } 2>&1 1>&3)"; } 3>&1; \
RETURN=$$?; \
$(ERR) "$$($(SED) -e 's/^/error: /' <<< "$$ERROR_OUTPUT")"; \
if [ $$RETURN -ne 0 ]; then \
	UNVERIFIED_FILE=$(@:o=o.unverified) ; \
	$(MV) '$(2)' "$$UNVERIFIED_FILE"; \
	$(ERR) "error: Moved object file $(2) with verifier failures to $$PWD/$$UNVERIFIED_FILE"; \
	exit $$RETURN; \
fi;
endef


#
# Empty the install lists of non-host and non-exclave headers if building the
# host headers alias and exclave headers aliases. Separately, export headers
# are also emptied for exclaves (but not for host headers) with
# EXPORTHDRS_SKIP_EXCLAVES.
#

ifeq ($(INSTALLHDRS_SKIP_HOST),YES)

INSTALL_MI_LIST =
INSTALL_MI_GEN_LIST =
INSTALL_IF_MI_LIST =
INSTALL_IF_MI_GEN_LIST =
INSTALL_DRIVERKIT_MI_LIST =
INSTALL_DRIVERKIT_MI_GEN_LIST =
INSTALL_KF_MI_LIST =
INSTALL_KF_MI_GEN_LIST =
INSTALL_MI_LCL_LIST =
INSTALL_MI_LCL_GEN_LIST =
INSTALL_IF_MI_LCL_LIST =
INSTALL_IF_MI_LCL_GEN_LIST =
INSTALL_SF_MI_LCL_LIST =
INSTALL_SF_MI_LCL_GEN_LIST =
INSTALL_DRIVERKIT_MI_LCL_LIST =
INSTALL_DRIVERKIT_MI_LCL_GEN_LIST =
INSTALL_KF_MI_LCL_LIST =
INSTALL_KF_MI_LCL_GEN_LIST =
INSTALL_MD_LIST =
INSTALL_MD_GEN_LIST =
INSTALL_DRIVERKIT_MD_LIST =
INSTALL_DRIVERKIT_MD_GEN_LIST =
INSTALL_KF_MD_LIST =
INSTALL_KF_MD_GEN_LIST =
INSTALL_MD_LCL_LIST =
INSTALL_MD_LCL_GEN_LIST =
INSTALL_SF_MD_LCL_LIST =
INSTALL_SF_MD_LCL_GEN_LIST =
INSTALL_DRIVERKIT_MD_LCL_LIST =
INSTALL_DRIVERKIT_MD_LCL_GEN_LIST =
INSTALL_KF_MD_LCL_LIST =
INSTALL_KF_MD_LCL_GEN_LIST =
INSTALL_KF_LIBCXX_MI_LIST =

# If there are no headers then there can be no module maps.
INSTALLHDRS_MODULEMAPS_SKIP_HOST = YES

endif

ifeq ($(INSTALLHDRS_MODULEMAPS_SKIP_HOST),YES)
INSTALL_MODULEMAP_MI_LIST =
INSTALL_MODULEMAP_MI_LCL_LIST =
INSTALL_MODULEMAP_MD_LIST =
INSTALL_MODULEMAP_MD_LCL_LIST =
endif

#
# Machine-independent (public) files
#

INSTALL_MI_INC_FILES = $(addprefix $(DSTROOT)/$(INCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_MI_LIST))
INSTALL_MI_INC_GEN_FILES = $(addprefix $(DSTROOT)/$(INCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_MI_GEN_LIST))
INSTALL_MODULEMAP_MI_INC_FILES = $(addprefix $(DSTROOT)/$(INCDIR)/$(INSTALL_MODULEMAP_MI_DIR)/,$(INSTALL_MODULEMAP_MI_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MI_INC_FILES),,incmidir,$(SINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MI_INC_GEN_FILES),1,incmigendir,$(SINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MODULEMAP_MI_INC_FILES),,mmincmidir,-t $(DATA_UNIFDEF)))

INSTALL_IF_MI_FILES = $(addprefix $(DSTROOT)/$(IINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_IF_MI_LIST))
INSTALL_IF_MI_GEN_FILES = $(addprefix $(DSTROOT)/$(IINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_IF_MI_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_IF_MI_FILES),,iincmidir,$(SINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_IF_MI_GEN_FILES),1,iincmigendir,$(SINCFRAME_UNIFDEF)))

ifeq ($(DRIVERKIT),1)
INSTALL_DRIVERKIT_MI_INC_FILES = $(addprefix $(DSTROOT)/$(DRIVERKITINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_DRIVERKIT_MI_LIST))
INSTALL_DRIVERKIT_MI_INC_GEN_FILES = $(addprefix $(DSTROOT)/$(DRIVERKITINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_DRIVERKIT_MI_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_DRIVERKIT_MI_INC_FILES),,dkincmidir,$(DKINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_DRIVERKIT_MI_INC_GEN_FILES),1,dkincmigendir,$(DKINCFRAME_UNIFDEF)))
endif

ifeq ($(EXCLAVEKIT),1)
INSTALL_EXCLAVEKIT_MI_INC_FILES = $(addprefix $(DSTROOT)/$(EXCLAVEKITINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_EXCLAVEKIT_MI_LIST))
INSTALL_EXCLAVEKIT_MI_INC_GEN_FILES = $(addprefix $(DSTROOT)/$(EXCLAVEKITINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_EXCLAVEKIT_MI_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVEKIT_MI_INC_FILES),,ekincmidir,$(EKINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVEKIT_MI_INC_GEN_FILES),1,ekincmigendir,$(EKINCFRAME_UNIFDEF)))
endif

ifeq ($(EXCLAVECORE),1)
INSTALL_EXCLAVECORE_MI_INC_FILES = $(addprefix $(DSTROOT)/$(EXCLAVECOREINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_EXCLAVECORE_MI_LIST))
INSTALL_EXCLAVECORE_MI_INC_GEN_FILES = $(addprefix $(DSTROOT)/$(EXCLAVECOREINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_EXCLAVECORE_MI_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVECORE_MI_INC_FILES),,ecincmidir,$(ECINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVECORE_MI_INC_GEN_FILES),1,ecincmigendir,$(ECINCFRAME_UNIFDEF)))
endif

INSTALL_KF_MI_FILES = $(addprefix $(DSTROOT)/$(KINCDIR)/$(EXPORT_MI_DIR)/, $(INSTALL_KF_MI_LIST))
INSTALL_KF_MI_GEN_FILES = $(addprefix $(DSTROOT)/$(KINCDIR)/$(EXPORT_MI_DIR)/, $(INSTALL_KF_MI_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_KF_MI_FILES),,kincmidir,$(KINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_KF_MI_GEN_FILES),1,kincmigendir,$(KINCFRAME_UNIFDEF)))

INSTALL_LIBCXX_MI_FILES = $(addprefix $(DSTROOT)/$(KLIBCXXINCDIR)/, $(INSTALL_KF_LIBCXX_MI_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_LIBCXX_MI_FILES),,cxxmidir,$(LIBCXXINCFRAME_UNIFDEF)))

#
# Machine-independent local (private) files
#

INSTALL_MI_LCL_FILES = $(addprefix $(DSTROOT)/$(LCLDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_MI_LCL_LIST))
INSTALL_MI_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(LCLDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_MI_LCL_GEN_LIST))
INSTALL_MODULEMAP_MI_LCL_FILES = $(addprefix $(DSTROOT)/$(LCLDIR)/$(INSTALL_MODULEMAP_MI_DIR)/,$(INSTALL_MODULEMAP_MI_LCL_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MI_LCL_FILES),,pincmidir,$(SPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MI_LCL_GEN_FILES),1,pincmigendir,$(SPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MODULEMAP_MI_LCL_FILES),,mmpincmidir,-t $(PDATA_UNIFDEF)))

INSTALL_IF_MI_LCL_FILES = $(addprefix $(DSTROOT)/$(IPINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_IF_MI_LCL_LIST))
INSTALL_IF_MI_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(IPINCDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_IF_MI_LCL_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_IF_MI_LCL_FILES),,ipincmidir,$(SPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_IF_MI_LCL_GEN_FILES),1,ipincmigendir,$(SPINCFRAME_UNIFDEF)))

INSTALL_SF_MI_LCL_FILES = $(addprefix $(DSTROOT)/$(SPINCDIR)/$(INSTALL_MI_DIR)/, $(sort $(INSTALL_SF_MI_LCL_LIST)))
INSTALL_SF_MI_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(SPINCDIR)/$(INSTALL_MI_DIR)/, $(sort $(INSTALL_SF_MI_LCL_GEN_LIST)))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_SF_MI_LCL_FILES),,spincmidir,$(SPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_SF_MI_LCL_GEN_FILES),1,spincmigendir,$(SPINCFRAME_UNIFDEF)))

ifeq ($(DRIVERKIT),1)
INSTALL_DRIVERKIT_MI_LCL_FILES = $(addprefix $(DSTROOT)/$(DRIVERKITLCLDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_DRIVERKIT_MI_LCL_LIST))
INSTALL_DRIVERKIT_MI_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(DRIVERKITLCLDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_DRIVERKIT_MI_LCL_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_DRIVERKIT_MI_LCL_FILES),,dkpincmidir,$(DKPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_DRIVERKIT_MI_LCL_GEN_FILES),1,dkpincmigendir,$(DKPINCFRAME_UNIFDEF)))
endif

ifeq ($(EXCLAVEKIT),1)
INSTALL_EXCLAVEKIT_MI_LCL_FILES = $(addprefix $(DSTROOT)/$(EXCLAVEKITLCLDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_EXCLAVEKIT_MI_LCL_LIST))
INSTALL_EXCLAVEKIT_MI_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(EXCLAVEKITLCLDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_EXCLAVEKIT_MI_LCL_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVEKIT_MI_LCL_FILES),,ekpincmidir,$(EKPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVEKIT_MI_LCL_GEN_FILES),1,ekpincmigendir,$(EKPINCFRAME_UNIFDEF)))
endif

ifeq ($(EXCLAVECORE),1)
INSTALL_EXCLAVECORE_MI_LCL_FILES = $(addprefix $(DSTROOT)/$(EXCLAVECORELCLDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_EXCLAVECORE_MI_LCL_LIST))
INSTALL_EXCLAVECORE_MI_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(EXCLAVECORELCLDIR)/$(INSTALL_MI_DIR)/, $(INSTALL_EXCLAVECORE_MI_LCL_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVECORE_MI_LCL_FILES),,ecpincmidir,$(ECPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVECORE_MI_LCL_GEN_FILES),1,ecpincmigendir,$(ECPINCFRAME_UNIFDEF)))
endif

INSTALL_KF_MI_LCL_FILES = $(addprefix $(DSTROOT)/$(KPINCDIR)/$(EXPORT_MI_DIR)/, $(INSTALL_KF_MI_LCL_LIST))
INSTALL_KF_MI_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(KPINCDIR)/$(EXPORT_MI_DIR)/, $(INSTALL_KF_MI_LCL_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_KF_MI_LCL_FILES),,kpincmidir,$(KPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_KF_MI_LCL_GEN_FILES),1,kpincmigendir,$(KPINCFRAME_UNIFDEF)))

#
# Machine-dependent (public) files
#

INSTALL_MD_INC_FILES = $(addprefix $(DSTROOT)/$(INCDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_MD_LIST))
INSTALL_MD_INC_GEN_FILES = $(addprefix $(DSTROOT)/$(INCDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_MD_GEN_LIST))
INSTALL_MODULEMAP_MD_INC_FILES = $(addprefix $(DSTROOT)/$(INCDIR)/$(INSTALL_MODULEMAP_MD_DIR)/,$(INSTALL_MODULEMAP_MD_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MD_INC_FILES),,incdir,$(SINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MD_INC_GEN_FILES),1,incgendir,$(SINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MODULEMAP_MD_INC_FILES),,mmincdir,-t $(DATA_UNIFDEF)))

ifeq ($(DRIVERKIT),1)
INSTALL_DRIVERKIT_MD_INC_FILES = $(addprefix $(DSTROOT)/$(DRIVERKITINCDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_DRIVERKIT_MD_LIST))
INSTALL_DRIVERKIT_MD_INC_GEN_FILES = $(addprefix $(DSTROOT)/$(DRIVERKITINCDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_DRIVERKIT_MD_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_DRIVERKIT_MD_INC_FILES),,dkincdir,$(DKINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_DRIVERKIT_MD_INC_GEN_FILES),1,dkincgendir,$(DKINCFRAME_UNIFDEF)))
endif

ifeq ($(EXCLAVEKIT),1)
INSTALL_EXCLAVEKIT_MD_INC_FILES = $(addprefix $(DSTROOT)/$(EXCLAVEKITINCDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_EXCLAVEKIT_MD_LIST))
INSTALL_EXCLAVEKIT_MD_INC_GEN_FILES = $(addprefix $(DSTROOT)/$(EXCLAVEKITINCDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_EXCLAVEKIT_MD_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVEKIT_MD_INC_FILES),,ekincdir,$(EKINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVEKIT_MD_INC_GEN_FILES),1,ekincgendir,$(EKINCFRAME_UNIFDEF)))
endif

ifeq ($(EXCLAVECORE),1)
INSTALL_EXCLAVECORE_MD_INC_FILES = $(addprefix $(DSTROOT)/$(EXCLAVECOREINCDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_EXCLAVECORE_MD_LIST))
INSTALL_EXCLAVECORE_MD_INC_GEN_FILES = $(addprefix $(DSTROOT)/$(EXCLAVECOREINCDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_EXCLAVECORE_MD_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVECORE_MD_INC_FILES),,ecincdir,$(ECINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVECORE_MD_INC_GEN_FILES),1,ecincgendir,$(ECINCFRAME_UNIFDEF)))
endif

INSTALL_KF_MD_FILES = $(addprefix $(DSTROOT)/$(KINCDIR)/$(EXPORT_MD_DIR)/, $(INSTALL_KF_MD_LIST))
INSTALL_KF_MD_GEN_FILES = $(addprefix $(DSTROOT)/$(KINCDIR)/$(EXPORT_MD_DIR)/, $(INSTALL_KF_MD_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_KF_MD_FILES),,kincdir,$(KINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_KF_MD_GEN_FILES),1,kincgendir,$(KINCFRAME_UNIFDEF)))

#
# Machine-dependent local (private) files
#

INSTALL_MD_LCL_FILES = $(addprefix $(DSTROOT)/$(LCLDIR)/$(INSTALL_MD_DIR)/, $(sort $(INSTALL_MD_LCL_LIST)))
INSTALL_MD_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(LCLDIR)/$(INSTALL_MD_DIR)/, $(sort $(INSTALL_MD_LCL_GEN_LIST)))
INSTALL_MODULEMAP_MD_LCL_FILES = $(addprefix $(DSTROOT)/$(LCLDIR)/$(INSTALL_MODULEMAP_MD_DIR)/,$(INSTALL_MODULEMAP_MD_LCL_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MD_LCL_FILES),,pincdir,$(SPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MD_LCL_GEN_FILES),1,pincgendir,$(SPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_MODULEMAP_MD_LCL_FILES),,mmpincdir,-t $(PDATA_UNIFDEF)))

INSTALL_SF_MD_LCL_FILES = $(addprefix $(DSTROOT)/$(SPINCDIR)/$(INSTALL_MD_DIR)/, $(sort $(INSTALL_SF_MD_LCL_LIST)))
INSTALL_SF_MD_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(SPINCDIR)/$(INSTALL_MD_DIR)/, $(sort $(INSTALL_SF_MD_LCL_GEN_LIST)))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_SF_MD_LCL_FILES),,spincdir,$(SPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_SF_MD_LCL_GEN_FILES),1,spincgendir,$(SPINCFRAME_UNIFDEF)))

ifeq ($(DRIVERKIT),1)
INSTALL_DRIVERKIT_MD_LCL_FILES = $(addprefix $(DSTROOT)/$(DRIVERKITLCLDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_DRIVERKIT_MD_LCL_LIST))
INSTALL_DRIVERKIT_MD_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(DRIVERKITLCLDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_DRIVERKIT_MD_LCL_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_DRIVERKIT_MD_LCL_FILES),,dkpincdir,$(DKPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_DRIVERKIT_MD_LCL_GEN_FILES),1,dkpincgendir,$(DKPINCFRAME_UNIFDEF)))
endif

ifeq ($(EXCLAVEKIT),1)
INSTALL_EXCLAVEKIT_MD_LCL_FILES = $(addprefix $(DSTROOT)/$(EXCLAVEKITLCLDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_EXCLAVEKIT_MD_LCL_LIST))
INSTALL_EXCLAVEKIT_MD_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(EXCLAVEKITLCLDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_EXCLAVEKIT_MD_LCL_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVEKIT_MD_LCL_FILES),,ekpincdir,$(EKPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVEKIT_MD_LCL_GEN_FILES),1,ekpincgendir,$(EKPINCFRAME_UNIFDEF)))
endif

ifeq ($(EXCLAVECORE),1)
INSTALL_EXCLAVECORE_MD_LCL_FILES = $(addprefix $(DSTROOT)/$(EXCLAVECORELCLDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_EXCLAVECORE_MD_LCL_LIST))
INSTALL_EXCLAVECORE_MD_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(EXCLAVECORELCLDIR)/$(INSTALL_MD_DIR)/, $(INSTALL_EXCLAVECORE_MD_LCL_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVECORE_MD_LCL_FILES),,ecpincdir,$(ECPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_EXCLAVECORE_MD_LCL_GEN_FILES),1,ecpincgendir,$(ECPINCFRAME_UNIFDEF)))
endif

INSTALL_KF_MD_LCL_FILES = $(addprefix $(DSTROOT)/$(KPINCDIR)/$(EXPORT_MD_DIR)/, $(INSTALL_KF_MD_LCL_LIST))
INSTALL_KF_MD_LCL_GEN_FILES = $(addprefix $(DSTROOT)/$(KPINCDIR)/$(EXPORT_MD_DIR)/, $(INSTALL_KF_MD_LCL_GEN_LIST))

$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_KF_MD_LCL_FILES),,kpincdir,$(KPINCFRAME_UNIFDEF)))
$(eval $(call INSTALLHDRS_RULE_template,$(INSTALL_KF_MD_LCL_GEN_FILES),1,kpincgendir,$(KPINCFRAME_UNIFDEF)))

.PHONY: do_installhdrs_mi

# Double-colon rule so that MakeInc.kernel can add custom behaviors
do_installhdrs_mi:: $(INSTALL_MI_INC_FILES) $(INSTALL_MI_INC_GEN_FILES) $(INSTALL_MODULEMAP_MI_INC_FILES) \
            $(INSTALL_IF_MI_FILES) $(INSTALL_IF_MI_GEN_FILES) \
		    $(INSTALL_DRIVERKIT_MI_INC_FILES) $(INSTALL_DRIVERKIT_MI_INC_GEN_FILES) \
		    $(INSTALL_EXCLAVEKIT_MI_INC_FILES) $(INSTALL_EXCLAVEKIT_MI_INC_GEN_FILES) \
			$(INSTALL_EXCLAVECORE_MI_INC_FILES) $(INSTALL_EXCLAVECORE_MI_INC_GEN_FILES) \
		    $(INSTALL_KF_MI_FILES) $(INSTALL_KF_MI_GEN_FILES) \
		    $(INSTALL_MI_LCL_FILES) $(INSTALL_MI_LCL_GEN_FILES) $(INSTALL_MODULEMAP_MI_LCL_FILES) \
            $(INSTALL_IF_MI_LCL_FILES) $(INSTALL_IF_MI_LCL_GEN_FILES) \
		    $(INSTALL_SF_MI_LCL_FILES) $(INSTALL_SF_MI_LCL_GEN_FILES) \
		    $(INSTALL_DRIVERKIT_MI_LCL_FILES) $(INSTALL_DRIVERKIT_MI_LCL_GEN_FILES) \
		    $(INSTALL_KF_MI_LCL_FILES) $(INSTALL_KF_MI_LCL_GEN_FILES) \
			$(INSTALL_LIBCXX_MI_FILES) \
			$(INSTALL_EXCLAVEKIT_MI_LCL_FILES) $(INSTALL_EXCLAVEKIT_MI_LCL_GEN_FILES) \
			$(INSTALL_EXCLAVECORE_MI_LCL_FILES) $(INSTALL_EXCLAVECORE_MI_LCL_GEN_FILES)
	@:

.PHONY: do_installhdrs_md

do_installhdrs_md: $(INSTALL_MD_INC_FILES) $(INSTALL_MD_INC_GEN_FILES) $(INSTALL_MODULEMAP_MD_INC_FILES) \
		   $(INSTALL_DRIVERKIT_MD_INC_FILES) $(INSTALL_DRIVERKIT_MD_INC_GEN_FILES) \
		   $(INSTALL_EXCLAVEKIT_MD_INC_FILES) $(INSTALL_EXCLAVEKIT_MD_INC_GEN_FILES) \
		   $(INSTALL_EXCLAVECORE_MD_INC_FILES) $(INSTALL_EXCLAVECORE_MD_INC_GEN_FILES) \
		   $(INSTALL_KF_MD_FILES) $(INSTALL_KF_MD_GEN_FILES) \
		   $(INSTALL_MD_LCL_FILES) $(INSTALL_MD_LCL_GEN_FILES) $(INSTALL_MODULEMAP_MD_LCL_FILES) \
		   $(INSTALL_SF_MD_LCL_FILES) $(INSTALL_SF_MD_LCL_GEN_FILES) \
		   $(INSTALL_DRIVERKIT_MD_LCL_FILES) $(INSTALL_DRIVERKIT_MD_LCL_GEN_FILES) \
		   $(INSTALL_EXCLAVEKIT_MD_LCL_FILES) $(INSTALL_EXCLAVEKIT_MD_LCL_GEN_FILES) \
		   $(INSTALL_EXCLAVECORE_MD_LCL_FILES) $(INSTALL_EXCLAVECORE_MD_LCL_GEN_FILES) \
		   $(INSTALL_KF_MD_LCL_FILES) $(INSTALL_KF_MD_LCL_GEN_FILES)
	@:

#
# Generic Export rules
#
EXP_MI_INC_DIR = $(OBJROOT)/$(EXPDIR)/$(EXPORT_MI_DIR)
EXPORT_MI_INC_FILES = $(addprefix $(EXP_MI_INC_DIR)/, $(EXPORT_MI_LIST))
EXPORT_MI_GEN_INC_FILES = $(addprefix $(EXP_MI_INC_DIR)/, $(EXPORT_MI_GEN_LIST))

$(EXP_MI_INC_DIR)::
	$(_v)$(MKDIR) $@

$(EXPORT_MI_GEN_INC_FILES): $(EXP_MI_INC_DIR)/% : % | $(EXP_MI_INC_DIR)
	$(_v)$(INSTALL) $(DATA_INSTALL_FLAGS_RO) $< $@

$(EXPORT_MI_INC_FILES): $(EXP_MI_INC_DIR)/% : $(SOURCE)/% | $(EXP_MI_INC_DIR)
	$(_v)$(INSTALL) $(DATA_INSTALL_FLAGS_RO) $< $@

EXP_MD_INC_DIR = $(OBJROOT)/$(EXPDIR)/$(EXPORT_MD_DIR)
EXPORT_MD_INC_FILES = $(addprefix $(EXP_MD_INC_DIR)/, $(EXPORT_MD_LIST))
EXPORT_MD_GEN_INC_FILES = $(addprefix $(EXP_MD_INC_DIR)/, $(EXPORT_MD_GEN_LIST))

$(EXP_MD_INC_DIR)::
	$(_v)$(MKDIR) $@

$(EXPORT_MD_GEN_INC_FILES): $(EXP_MD_INC_DIR)/% : % | $(EXP_MD_INC_DIR)
	$(_v)$(INSTALL) $(DATA_INSTALL_FLAGS_RO) $< $@

$(EXPORT_MD_INC_FILES): $(EXP_MD_INC_DIR)/% : $(SOURCE)/% | $(EXP_MD_INC_DIR)
	$(_v)$(INSTALL) $(DATA_INSTALL_FLAGS_RO) $< $@

.PHONY: do_exporthdrs_mi

do_exporthdrs_mi: $(EXPORT_MI_GEN_INC_FILES) $(EXPORT_MI_INC_FILES) | $(EXP_MI_INC_DIR)
	@:

.PHONY: do_exporthdrs_md

do_exporthdrs_md: $(EXPORT_MD_GEN_INC_FILES)  $(EXPORT_MD_INC_FILES) | $(EXP_MD_INC_DIR)
	@:

#
# Generic Compilation rules
#

#
# Compilation rules to generate .o from .s
#

S_RULE_0=@$(LOG_AS) "$@"
S_RULE_1A=$(_v)$(S_KCC) -c $(SFLAGS) -MD -MF $(@:o=d) -MJ $(@:o=o.json) -MP $($@_SFLAGS_ADD) $(INCFLAGS) $($@_INCFLAGS)
S_RULE_1B=$(<F)
S_RULE_2=

#
# Compilation rules to generate .o from .c for normal files
#
C_RULE_0=@$(LOG_CC) "$@"
C_RULE_1A=$(_v)$(KCC) -c $(filter-out $($@_CFLAGS_RM), $(CFLAGS) $(CWARNFLAGS)) -MD -MF $(@:o=d) -MJ $(@:o=o.json) -MP $($@_CFLAGS_ADD) $($@_CWARNFLAGS_ADD) $(INCFLAGS) $($@_INCFLAGS)
C_RULE_1B=$(<F)
C_RULE_2=
C_RULE_3=
C_RULE_4=


#
# Compilation rules to generate .o from .c for driver files
#
C_RULE_0_D=$(C_RULE_0)
C_RULE_1A_D=$(C_RULE_1A)
C_RULE_1B_D=$(C_RULE_1B)
C_RULE_2_D=$(C_RULE_2)
C_RULE_3_D=$(C_RULE_3)
C_RULE_4_D=$(C_RULE_4)

#
# Compilation rules to generate .co from .cp or .cpo from .cpp
#   The config tool slickly changes the last source filename char to 'o'
#   for the object filename.
P_RULE_0=@$(LOG_CXX) "$@"
P_RULE_1A=$(_v)$(KC++) -o $@ -c $(CXXFLAGS) $(filter-out $($@_CFLAGS_RM), $(CFLAGS) $(CXXWARNFLAGS)) -MD -MF $(@:o=d) -MJ $(@:o=o.json) -MP $($@_CFLAGS_ADD) $($@_CXXWARNFLAGS_ADD) $(INCFLAGS) $($@_INCFLAGS)
P_RULE_1B=$(<F)
P_RULE_2=

P_RULE_3=@$(if $($@_VERIFIER), $(LOG_VERIFIER) "$@")
P_RULE_4=@$(if $($@_VERIFIER), $(call VERIFY,$($@_VERIFIER),$@))

#
# Explain how to make the bsd syscall list
#
$(TARGET)/bsd.syscalls.master: $(SRCROOT)/bsd/kern/syscalls.master $(MAKESYSCALLS)
	@$(LOG_GENERATE) "$(@F)$(Color0) from $(ColorF)$<$(Color0)"
	$(_v)$(UNIFDEF) $(SEED_DEFINES) $< > $@;	\
	if [ $$? -eq 2 ]; then				\
		$(ERR) Parse failure for $<;		\
		exit 1;					\
	fi

.PHONY: do_build_all

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_build_all::
	@:

.PHONY: do_build_install_primary

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_build_install_primary::
	@:

.PHONY: do_build_install_non_primary

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_build_install_non_primary::
	@:

INSTALL_MAN_FILES = $(addprefix $(DSTROOT)/$(MANDIR)/$(INSTALL_MAN_DIR)/, $(INSTALL_MAN_LIST))

.PHONY: INSTALL_MAN_DIR

INSTALL_MAN_DIR:
	$(_v)$(MKDIR) $(DSTROOT)/$(MANDIR)/$(INSTALL_MAN_DIR)

$(INSTALL_MAN_FILES): $(DSTROOT)/$(MANDIR)/$(INSTALL_MAN_DIR)/% : % | INSTALL_MAN_DIR
	@$(LOG_MAN) "$*"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@

define MAN_LINKS_RULE_template
$$(DSTROOT)/$$(MANDIR)/$$(INSTALL_MAN_DIR)/$(2): $$(DSTROOT)/$$(MANDIR)/$$(INSTALL_MAN_DIR)/$(1)
	@$$(LOG_MANLINK) "$(2)"
	$(_v)ln -f $$< $$@
endef

function_generate_man_links_rules = $(if $(word 1,$(1)),$(eval $(call MAN_LINKS_RULE_template,$(word 1,$(1)),$(word 2,$(1)))) $(DSTROOT)/$(MANDIR)/$(INSTALL_MAN_DIR)/$(word 2,$(1)) $(call function_generate_man_links_rules,$(wordlist 3,$(words $(1)),$(1))))

INSTALL_MAN_FILES_LINKS = $(call function_generate_man_links_rules,$(INSTALL_MAN_LINKS))

.PHONY: do_installman

do_installman: $(INSTALL_MAN_FILES) $(INSTALL_MAN_FILES_LINKS)
	@:

.PHONY: do_textfiles_install do_textfiles_install_mi do_textfiles_install_md

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_textfiles_install_mi:: do_installman
	@:

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_textfiles_install_md::
	@:

.PHONY: do_build_setup

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_build_setup::
	@:

.PHONY: do_config_all

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_config_all::
	@:

.PHONY: do_config_install_primary

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_config_install_primary::
	@:

.PHONY: do_config_install_variant

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_config_install_variant::
	@:

.PHONY: do_config_install

# Do-nothing rule, since not all levels of the recursive hierarchy might implement this
# in their local Makefiles. Those that do will use a "::" rule to augment this.
do_config_install::
	@:

# vim: set ft=make:
